package ch.nostromo.tiffanys.engines.tiffanys.board.pieces;

import ch.nostromo.tiffanys.engines.tiffanys.board.TiffanysBoard;
import ch.nostromo.tiffanys.engines.tiffanys.board.TiffanysMove;
import ch.nostromo.tiffanys.engines.tiffanys.board.pieces.TiffanysPiece;
import ch.nostromo.tiffanys.game.board.Board;

public class TiffanysPawn extends TiffanysPiece {

  private static final int myPieceCode = Board.PIECE_PAWN;

  public static final int MATERIAL_VALUE = 100;

  public static int getMoveList(long[] board, int currentPosition, int myColor, int oldArrayLength, TiffanysMove[] moves, int[] enemyValues) {
    int arrayLength = oldArrayLength;
    int direction = 1;

    if (myColor == Board.OWNER_BLACK)
      direction = -1;

    // One step forward (including possible "pawn promotion")
    int toField = currentPosition + (10 * direction);
    if ((Board.EMPTY_FIELD & board[toField]) != 0) {
      if (toField > 90 || toField < 29) {
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_KNIGHT);
        arrayLength++;
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_QUEEN);
        arrayLength++;
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_ROOK);
        arrayLength++;
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_BISHOP);
        arrayLength++;
      } else {
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode);
        arrayLength++;
      }
    }

    // Two steps forward
    int toField1 = toField;
    toField = currentPosition + (20 * direction);
    if (!TiffanysBoard.isMovedField(board[currentPosition]) && TiffanysBoard.isEmptyField(board[toField1]) && TiffanysBoard.isEmptyField(board[toField])) {
      moves[arrayLength].setMove(currentPosition, toField, myPieceCode, myPieceCode, toField1);
      arrayLength++;
    }

    // hit Moves ...
    toField = currentPosition + (11 * direction);
    if (enemyValues[toField] != 0) {
      if (toField > 90 || toField < 29) {
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_KNIGHT);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_QUEEN);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_ROOK);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_BISHOP);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
      
      } else {
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
      }
    }

    // also for enpassant
    if (toField == board[Board.IDX_ENPASSANT_FIELD]) {
      moves[arrayLength].setMove(currentPosition, toField, myPieceCode);
      moves[arrayLength].enPassantMove = true;
      moves[arrayLength].enPassantOpponentTake = toField + (direction * -10);
      moves[arrayLength].hitmove = true;
      moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
      arrayLength++;
    }

    // hit Moves to the other side
    toField = currentPosition + (9 * direction);
    if (enemyValues[toField] != 0) {
      if (toField > 90 || toField < 29) {
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_KNIGHT);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_QUEEN);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_ROOK);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_BISHOP);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
      } else {
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
      }

    }
    // also for enpassant
    if (toField == board[Board.IDX_ENPASSANT_FIELD]) {
      moves[arrayLength].setMove(currentPosition, toField, myPieceCode);
      moves[arrayLength].enPassantMove = true;
      moves[arrayLength].enPassantOpponentTake = toField + (direction * -10);
      moves[arrayLength].hitmove = true;
      moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
      arrayLength++;
    }

    return arrayLength;

  }


  public static int getHitMoveList(long[] board, int currentPosition, int myColor, int oldArrayLength, TiffanysMove[] moves, int[] enemyValues) {
    int arrayLength = oldArrayLength;
    int direction = 1;

    if (myColor == Board.OWNER_BLACK)
      direction = -1;

    
    // hit Moves ...
    int toField = currentPosition + (11 * direction);
    if (enemyValues[toField] != 0) {
      if (toField > 90 || toField < 29) {
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_KNIGHT);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_QUEEN);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_ROOK);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_BISHOP);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
      } else {
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
      }
    }

    // also for enpassant
    if (toField == board[Board.IDX_ENPASSANT_FIELD]) {
      moves[arrayLength].setMove(currentPosition, toField, myPieceCode);
      moves[arrayLength].enPassantMove = true;
      moves[arrayLength].enPassantOpponentTake = toField + (direction * -10);
      moves[arrayLength].hitmove = true;
      moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
      arrayLength++;
    }

    // hit Moves to the other side
    toField = currentPosition + (9 * direction);
    if (LEGALBOARD[toField] && TiffanysBoard.isEnemyPiece(board[toField], myColor)) {
      if (toField > 90 || toField < 29) {
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_KNIGHT);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_QUEEN);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_ROOK);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode, Board.PIECE_BISHOP);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
      } else {
        moves[arrayLength].setMove(currentPosition, toField, myPieceCode);
        moves[arrayLength].hitmove = true;
        moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
        arrayLength++;
      }

    }
    // also for enpassant
    if (toField == board[Board.IDX_ENPASSANT_FIELD]) {
      moves[arrayLength].setMove(currentPosition, toField, myPieceCode);
      moves[arrayLength].enPassantMove = true;
      moves[arrayLength].enPassantOpponentTake = toField + (direction * -10);
      moves[arrayLength].hitmove = true;
      moves[arrayLength].hitScore = enemyValues[toField] - MATERIAL_VALUE;
      arrayLength++;
    }

    return arrayLength;

  }


}